package com.dhecp.project.common.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.controller.BaseController;
import com.dhecp.core.utils.Kdniao.KdniaoResult;
import com.dhecp.core.utils.Kdniao.KdniaoUtils;

/**
 * 快递鸟对接通用接口
 * @author DH-Xxj
 * @version 2020年9月14日
 */
@Controller
@RequestMapping("/common/kdniao")
public class KdniaoController extends BaseController {
	/**
     *打开物流轨迹页面
     * @return ModelAndView
     */
	@GetMapping()
	public ModelAndView foldRelationPage(@RequestParam Map<String, Object> params){
		ModelAndView mv = this.getModelAndView();
        mv.addObject("prefix", "/common/kdniao");
		mv.setViewName("common/kdniao");
        return mv;
	}
    /**
     * 查询订单物流轨迹
     * @param expCode 快递公司对应编码
     * @param expNo  快递单号
     * @param client 寄件人或收件人手机号（主要后四位）
     * @return KdniaoResult
     * @author DH-Xxj
     */
    @GetMapping("/wlcx")
    @ResponseBody
    @RepeatSubmit
    public KdniaoResult kdniaoResult(@RequestParam Map<String, Object> params){
		String expCode = "";
		String expNo = "";
		String client = "";
		KdniaoResult kdniaoResult = new KdniaoResult();   

		if (params.get("expCode") != null && params.get("expCode") != ""){
			expCode = params.get("expCode").toString().replaceAll("\\s*", "");
		}
		if (params.get("expNo") != null && params.get("expNo") != ""){
			expNo = params.get("expNo").toString().replaceAll("\\s*", "");
		}
		if (params.get("client") != null && params.get("client") != ""){
			client = params.get("client").toString().replaceAll("\\s*", "");
			client = client.substring(client.length() - 4, client.length());
		}
		
        try {
        	kdniaoResult = KdniaoUtils.getKdniaoTraces(expCode, expNo, client);
        }catch (Exception e) {
        	kdniaoResult.setState("0");
        	kdniaoResult.setReason(this.getExceptionMessage(e));
        }
        return kdniaoResult;
    }
    
}
